/*
https://leetcode-cn.com/problems/maximum-xor-of-two-numbers-in-an-array/solution/shu-zu-zhong-liang-ge-shu-de-zui-da-yi-h-n9m9/
 */
import java.util.HashSet;
import java.util.Set;

public class Solution421 {
    public int findMaximumXOR(int[] nums) {
        int ans=0;
        for (int i=30;i>=0;i--){
            Set<Integer> set=new HashSet<>();
            for (int num : nums) {
                set.add(num>>i);
            }
            int x=ans*2+1;
            boolean flag=false;
            for (int num : nums) {
                if (set.contains((num>>i)^x)){
                    flag=true;
                    break;
                }
            }
            ans=flag?x:x-1;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution421().findMaximumXOR(new int[]{3,10,5,25,2,8}));
    }
}
